package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.OrderStatus;
import com.qianying.service.OrderStatusService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 订单状态Controller
 */
@RestController
@RequestMapping("/admin/orderStatus")
@Tag(name = "平台设置:订单状态管理", description = "订单状态信息的增删改查接口")
public class OrderStatusController {

    @Autowired
    private OrderStatusService orderStatusService;

    /**
     * 分页查询订单状态列表
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询订单状态列表",
            description = "支持按订单状态名称模糊查询，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码", example = "1"),
                    @Parameter(name = "size", description = "每页条数", example = "10"),
                    @Parameter(name = "statusName", description = "订单状态名称（模糊查询）", example = "订单待支付")
            }
    )
    public CommonResult<IPage<OrderStatus>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String statusName) {
        Page<OrderStatus> page = new Page<>(current, size);
        return orderStatusService.getPage(page, statusName);
    }

    /**
     * 根据ID查询订单状态
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询订单状态详情",
            description = "通过ID获取订单状态的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<OrderStatus> getById(@RequestParam Long id) {
        return orderStatusService.getById(id);
    }

    /**
     * 新增订单状态
     */
    @PostMapping("/saveStatus")
    @Operation(
            summary = "新增订单状态",
            description = "创建新的订单状态记录，订单状态名称和代号为必填项",
            parameters = {
                    @Parameter(name = "statusName", description = "订单状态名称", required = true, example = "订单待支付"),
                    @Parameter(name = "statusCode", description = "订单状态代号", required = true, example = "PENDING_PAYMENT"),
                    @Parameter(name = "description", description = "订单状态说明", example = "订单已创建但尚未支付")
            }
    )
    public CommonResult<?> saveStatus(@RequestBody OrderStatus orderStatus) {
        return orderStatusService.saveStatus(orderStatus);
    }

    /**
     * 更新订单状态
     */
    @PostMapping("/updateStatus")
    @Operation(
            summary = "更新订单状态",
            description = "修改已有订单状态信息，必须包含ID、订单状态名称和代号",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "statusName", description = "订单状态名称", required = true, example = "订单待支付"),
                    @Parameter(name = "statusCode", description = "订单状态代号", required = true, example = "PENDING_PAYMENT"),
                    @Parameter(name = "description", description = "订单状态说明", example = "订单已创建但尚未支付")
            }
    )
    public CommonResult<?> updateStatus(@RequestBody OrderStatus orderStatus) {
        return orderStatusService.updateStatus(orderStatus);
    }

    /**
     * 删除订单状态
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除订单状态",
            description = "通过ID删除指定的订单状态记录",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return orderStatusService.removeById(id);
    }

    /**
     * 查询所有订单状态（不分页）
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有订单状态",
            description = "返回所有订单状态列表，适用于下拉选择等场景",
            parameters = {}
    )
    public CommonResult<List<OrderStatus>> getAll() {
        return orderStatusService.getAll();
    }
}
